package demo6;

import java.util.Arrays;

/**
 * @Author: Lenovo
 * @Date: 2024/5/23 14:02
 * @description:
 */
class Money implements Cloneable{
    public double money=9.9;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
class Person implements Cloneable{
    @Override
    protected Object clone() throws CloneNotSupportedException {
        Person tmp=(Person) super.clone();
        tmp.m=(Money) this.m.clone();
        return tmp;
    }

    public String name;
    public int age;
    public Money m=new Money();
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }


}
public class Test {

    public static void main(String[] args)throws CloneNotSupportedException{
        Person person1=new Person("zhangsan",8);
        Person person2 = (Person)person1.clone();
        System.out.println("修改前:"+person1.m.money);
        System.out.println("修改前:"+person2.m.money);
        person2.m.money=99.99;
        System.out.println("修改后:"+person1.m.money);
        System.out.println("修改后:"+person2.m.money);

    }
    public static void main1(String[] args)throws CloneNotSupportedException {
        Person person1=new Person("zhangsan",8);

        Person person2=(Person) person1.clone();
        System.out.println(person2.toString());
    }
}
